package com.module.ble.db

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.RawQuery
import androidx.room.Update
import androidx.sqlite.db.SupportSQLiteQuery
import com.module.ble.db.table.UserCalorie
import kotlinx.coroutines.flow.Flow

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/09/17
 *
 * 描述：卡路里
 *
 * 修订历史：
 *
 */
@Dao
interface UserCalorieDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insert(vararg bean: UserCalorie)

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insert(mList: List<UserCalorie>)

    @Query("SELECT createdTime FROM user_calorie WHERE createdTime IN (:times)")
    fun getExistingTimes(times: List<Long>): List<Long>

    @Query("SELECT * FROM user_calorie")
    fun findAll(): MutableList<UserCalorie>

    @Query("SELECT * FROM user_calorie WHERE sysStat = 0")
    fun findAllNotUp(): MutableList<UserCalorie>

    @Query("SELECT * FROM user_calorie")
    fun findAllFlow(): Flow<List<UserCalorie>>

    // 先获取最新日期，再查询该日期的所有数据
    @Query("SELECT * FROM user_calorie WHERE date(createdTime/1000, 'unixepoch', 'localtime') = (SELECT date(MAX(createdTime)/1000, 'unixepoch', 'localtime') FROM user_calorie) ORDER BY createdTime DESC")
    fun findLatestDayRecords(): List<UserCalorie>

    @Query("SELECT * FROM user_calorie WHERE date(createdTime/1000, 'unixepoch', 'localtime') = (SELECT date(MAX(createdTime)/1000, 'unixepoch', 'localtime') FROM user_calorie) ORDER BY createdTime DESC")
    fun findLatestDayFlow(): Flow<List<UserCalorie>>

    //根据时间范围查询数据
    @Query("SELECT * FROM user_calorie WHERE createdTime >= :startTime AND createdTime <= :endTime ORDER BY createdTime ASC")
    fun findByTimeRange(startTime: Long, endTime: Long): List<UserCalorie>

    // 根据时间范围查询数据（Flow版本）
    @Query("SELECT * FROM user_calorie WHERE createdTime >= :startTime AND createdTime <= :endTime ORDER BY createdTime ASC")
    fun findByTimeRangeFlow(startTime: Long, endTime: Long): Flow<List<UserCalorie>>

    /**
     * 根据指定时间戳查询该时间戳当天内的所有数据
     * @param timestamp 指定的时间戳（毫秒）
     * @return List<UserCalorie>
     */
    @Query("""SELECT * FROM user_calorie 
             WHERE date(createdTime/1000, 'unixepoch', 'localtime') = date(:timestamp/1000, 'unixepoch', 'localtime') 
             ORDER BY createdTime ASC""")
    fun findByTimestampTheDay(timestamp: Long): List<UserCalorie>

    /**
     * 根据指定时间戳查询该时间戳当天内的所有数据（Flow版本）
     * @param timestamp 指定的时间戳（毫秒）
     * @return Flow<List<UserCalorie>>
     */
    @Query("""SELECT * FROM user_calorie 
             WHERE date(createdTime/1000, 'unixepoch', 'localtime') = date(:timestamp/1000, 'unixepoch', 'localtime') 
             ORDER BY createdTime ASC""")
    fun findByTimestampTheDayFlow(timestamp: Long): Flow<List<UserCalorie>>

    @RawQuery
    fun findByCustomQuery(query: SupportSQLiteQuery): List<UserCalorie>

    @Update
    fun update(msg: UserCalorie)

    @Update
    fun update(mList:List<UserCalorie>)

    @Query("select count(*) from user_calorie")
    fun count(): Int

    //查询某条数据
    @Query("SELECT * FROM user_calorie WHERE createdTime = :time")
    fun findMessageByTime(time: Long): UserCalorie

    //删除所有
    @Query("DELETE FROM user_calorie")
    fun deleteAll()

    @Query("DELETE FROM user_calorie WHERE createdTime = :time")
    fun deleteMsgByTime(time: Long)

    /**
     * 将所有数据的sysStat更新为0
     */
    @Query("UPDATE user_calorie SET sysStat = 0")
    fun updateAllSysStatToZero()
}